package com.gx.smart.construction.ui.open.fragment

import android.app.Activity
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.construction.R
import com.gx.smart.construction.databinding.OpenDoorFragmentBinding
import com.gx.smart.construction.ui.common.fragment.CommonSelectRoomFragment
import com.gx.smart.construction.ui.common.model.RoomName
import com.gx.smart.construction.ui.common.page.SearchRoomActivity
import com.gx.smart.construction.ui.open.viewmodel.Device
import com.gx.smart.construction.ui.open.viewmodel.OpenDoorViewModel
import com.gx.smart.lib.event.LoadingMessageEvent
import org.greenrobot.eventbus.EventBus

class OpenDoorFragment : CommonSelectRoomFragment<OpenDoorViewModel>(){
    private val roomSearchLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) {
        val resultCode = it.resultCode
        if (resultCode == Activity.RESULT_OK) {
            val number = it.data?.getStringExtra("number")
            val id = it.data?.getStringExtra("id")
            val roomName = RoomName()
            roomName.id = id
            roomName.content = number
            viewModel.changeRoomName(roomName)
        }
    }
    private val mQRCodeObserver:Observer<Device> = Observer { device ->
        EventBus.getDefault().post(LoadingMessageEvent(false))
        val content = device.qrCode
        if (!TextUtils.isEmpty(content)) {
            val qrCodeBitmap =
                com.yzq.zxinglibrary.encode.CodeCreator.createQRCode(content, 600, 600, null)
            com.gx.smart.lib.widget.dialog.CustomAlertDialog.with(requireActivity() as androidx.appcompat.app.AppCompatActivity)
                .setCancelable(false)
                .setBitmap(qrCodeBitmap)
                .show()
        } else {
            ToastUtils.showLong("二维码信息为空!")
        }
    }


    override fun onBindViewModel(): Class<OpenDoorViewModel> {
        return OpenDoorViewModel::class.java
    }

    override fun initContentView(root: ViewGroup?) {
        val binding: OpenDoorFragmentBinding = DataBindingUtil.inflate(
            layoutInflater,
            onBindLayout(),
            root,
            true
        )
        binding.lifecycleOwner = viewLifecycleOwner
        binding.selectRoomViewModel = viewModel
        binding.fragment = this
    }

    override fun initObserver() {
        viewModel!!.error.observe(this) { result ->
            if (result) {
                EventBus.getDefault().post(LoadingMessageEvent(false))
            }
        }
    }

    @Suppress("UNUSED_PARAMETER")
    fun selectArea(v:View){
        selectArea()
    }

    @Suppress("UNUSED_PARAMETER")
    fun selectBuilding(v:View){
        selectBuilding()
    }

    @Suppress("UNUSED_PARAMETER")
    fun selectUnit(v:View){
        selectUnit()
    }

    @Suppress("UNUSED_PARAMETER")
    fun selectRoom(v:View){
        val unitName = viewModel.unitName.value
        if (unitName == null) {
            ToastUtils.showLong("请先选择单元")
            return
        }
        val intent = Intent(activity, SearchRoomActivity::class.java)
        intent.putExtra("parentId", unitName.id)
        roomSearchLauncher.launch(intent)
    }

    @Suppress("UNUSED_PARAMETER")
    fun createQRCode(v:View){
        qrCode()
    }

    private fun qrCode() {
        if (commonHandle()) {
            val roomName = viewModel.roomName.value
            if (roomName != null) {
                viewModel.getQrCodePass(roomName.id.toLong()).observe(viewLifecycleOwner, mQRCodeObserver)
            }
        }
    }

    override fun onBindLayout(): Int {
        return R.layout.open_door_fragment
    }

    override fun getToolbarTitle(): String {
        return ""
    }

    override fun enableToolbar(): Boolean {
        return false
    }
}